package com.sjm.suchbear.easymap;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjm.suchbear.easymap.Model.LoginModel;
import com.sjm.suchbear.easymap.Service.iHttpService;
import com.sjm.suchbear.easymap.Util.DialogLoading;
import com.sjm.suchbear.easymap.Util.HttpUtils;
import com.sjm.suchbear.easymap.Util.SP;
import com.sjm.suchbear.easymap.Util.T;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;
import zilla.libcore.util.Util;

public class LoginActivity extends AppCompatActivity {

    @BindView(R.id.ll_header_back)
    LinearLayout llHeaderBack;
    @BindView(R.id.tv_list_title)
    TextView tvListTitle;
    @BindView(R.id.iv_header_function_ico)
    ImageView ivHeaderFunctionIco;
    @BindView(R.id.ll_header_function)
    LinearLayout llHeaderFunction;
    @BindView(R.id.ll_List_Main)
    RelativeLayout llListMain;
    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.tv_lostPwd)
    TextView tvLostPwd;
    @BindView(R.id.tv_login)
    TextView tvLogin;

    private iHttpService service;
    final private static int SUCCESS = 0;
    final private static int FAILD = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        service = HttpUtils.createService(this);
    }

    @OnClick({R.id.ll_header_back, R.id.tv_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_header_back:
                setResult(FAILD);
                finish();
                break;
            case R.id.tv_login:
                login();
                break;
        }
    }

    private void login() {
        final String username = String.valueOf(etUsername.getText());
        final String password = String.valueOf(etPassword.getText());
        if (username.equals("")) {
            T.showShort(getBaseContext(), "用户名不得为空");
            return;
        }
        if (password.equals("")) {
            T.showShort(getBaseContext(), "密码不得为空");
            return;
        }
        service.doLogin(username, password)
                .subscribeOn(Schedulers.io())//指定网络请求在IO线程
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        DialogLoading.StartLodingDialog(LoginActivity.this);
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())//显示Dialog在主线程中
                .observeOn(AndroidSchedulers.mainThread())//显示数据在主线程
                .subscribe(new Subscriber<LoginModel>() {
                    @Override
                    public void onCompleted() {
                        DialogLoading.StopLodingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        DialogLoading.StopLodingDialog();
                        Util.toastMsg("网络连接失败");
                    }

                    @Override
                    public void onNext(LoginModel loginModel) {
                        if (!loginModel.getUserId().equals("")) {
                            SP.setLoginUser(getBaseContext(), username, loginModel.getUserId(), password);
                            Util.toastMsg("登录成功");
                            setResult(SUCCESS);
                            finish();
                        } else {
                            Util.toastMsg(loginModel.getMsg());
                        }
                    }
                });
    }
}
